<template>
  <div class="sort_condition"
       :class="tab?'sort_condition_focus':''">
    <div class="operation_panel"
         @click="toggelFilterTabs">
      <div class="panel_left">
        <span class="panel_left_icon"></span>
        <span>{{$t('attribute.sort')}}</span>
      </div>
      <div class="panel_right">
        <span :class="clomu_sort.field?'asidSort_label':''">{{returnLabel}}</span>
        <span class="focus_pgup cuifont"
              :class="sort_order?'icon_arrow_up':'icon_arrow_down'"></span>
      </div>
    </div>
    <div class="panel_content"
         v-show="tab"
         @click.stop="">
      <div class="panel_content_select">
        <div class="pannel_select">
          <el-select v-model="clomu_sort.field"
                     popper-class="sort_select_box"
                     placeholder="Please select"
                     @change="selectChange">
            <el-option v-for="item in options"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
              <div class="opntion_label">
                <span>{{item.label}}</span>
                <span class="cuifont icon_checkmark"></span>
              </div>
            </el-option>
          </el-select>
        </div>
        <div>
          <div class="focus_pgup_box"
               @click="toggleSortOrder">
            <span class="pgup_sort"
                  :class="sort_order?'pgup_sort':'pgdn_sort'"></span>
          </div>
        </div>
      </div>
      <div class="back_default_sort"
           @click="backDefaultSort">{{$t('custom.component.viewComponent.backDefaultSort')}}</div>
    </div>
  </div>
</template>
<script>
export default {
  props: {
    options: {
      type: Array
    },
    conditionItem: {
      type: String
    }
  },
  computed: {
    returnLabel () {
      let label = ''
      for (let index = 0; index < this.options.length; index++) {
        const element = this.options[index]
        if (element.value === this.clomu_sort.field) {
          label = element.label
          break
        }
      }
      return label
    }
  },
  watch: {
    conditionItem: function (val) {
      if (val === 'sort') this.tab = true
      else this.tab = false
    }
  },
  data () {
    return {
      sort_order: false,
      tab: false,
      clomu_sort: {
        sort: 'DESC',
        field: 'name'
      }
    }
  },
  methods: {
    toggelFilterTabs () {
      this.tab = !this.tab
      this.$emit('tabTitle', {
        tab: this.tab,
        text: 'sort'
      })
    },
    selectChange () {
      this.$emit('sortChange', [this.clomu_sort])
    },
    toggleSortOrder () {
      this.sort_order = !this.sort_order
      this.clomu_sort.sort = !this.sort_order ? 'DESC' : 'ASC'
      this.$emit('sortChange', [this.clomu_sort])
    },
    backDefaultSort () {
      this.$set(this.clomu_sort, 'field', 'name')
      this.$set(this.clomu_sort, 'sort', 'DESC')
      this.sort_order = false
      this.$emit('sortChange', [this.clomu_sort])
    }

  }
}
</script>
<style lang="scss">
.sort_condition {
  cursor: pointer;
  margin-bottom: 4px;
  // height: 40px;
  .operation_panel {
    display: flex;
    justify-content: space-between;
    align-items: center;
    // height: 40px;
    height: 36px;
    padding: 6px 16px;
    .panel_left {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .panel_left_icon {
        width: 14px;
        height: 14px;
        display: inline-block;
        background-image: url("#{$--ossLink}/app/view_pageheader/toggle_sort.png");
        background-repeat: no-repeat;
        background-size: 14px;
        background-position: center;
        margin-right: 10px;
      }
    }
    .panel_right {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 24px;
      background: $--theme-color-background;
      border-radius: 4px;
      padding: 0 8px;
      cursor: pointer;
      .asidSort_label {
        margin-right: 8px;
        color: $--theme-color;
      }
      .focus_pgup {
        width: 12px;
        height: 12px;
        display: inline-block;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .focus_pgup::before {
        font-size: 12px;
        color: $--theme-color;
      }
    }
  }
  .operation_panel:focus {
    background-color: #f0f0f0;
  }
  .panel_content_focus {
  }
  .panel_content {
    padding: 0 16px;
    .panel_content_select {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      height: 36px;
      .pannel_select {
        height: 28px;
        width: 202px;
        .el-input__inner {
          height: 28px;
        }
        .el-input__icon {
          line-height: 28px;
        }
      }
      .focus_pgup_box {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 28px;
        height: 28px;
        border-radius: 4px;
        margin-left: 10px;
        cursor: pointer;
        .pgup_sort {
          display: inline-block;
          width: 14px;
          height: 14px;
          background-image: url("#{$--ossLink}/app/view_pageheader/pgup_sort.png");
          background-repeat: no-repeat;
          background-size: 14px;
          background-position: center;
        }
        .pgdn_sort {
          display: inline-block;
          width: 14px;
          height: 14px;
          background-image: url("#{$--ossLink}/app/view_pageheader/pgdn_sort.png");
          background-repeat: no-repeat;
          background-size: 14px;
          background-position: center;
        }
      }
      .focus_pgup_box:hover {
        background-color: #d8d8d8;
      }
    }
    .back_default_sort {
      margin-top: 5px;
      color: $--theme-color;
      cursor: pointer;
    }
    .back_default_sort:hover {
      text-decoration: underline;
    }
  }
}
.sort_condition_focus {
  background-color: #f6f7f9;
}
.sort_condition:hover {
  background-color: #f6f7f9;
}
.sort_select_box {
  .opntion_label {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .cuifont {
      display: none;
    }
    .cuifont::before {
      font-size: 14px;
      font-weight: 100;
    }
  }
  .selected .cuifont {
    display: block;
  }
}
</style>
